package views.fin;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.awt.event.MouseListener;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.text.MaskFormatter;

import models.cad.Usuario;
import models.fin.Categoria;
import models.fin.Conta;
import models.fin.Fornecedor;
import supers.GUICadInterface;
import supers.GUIInternalFrame;
import utils.GlobalDeclarations;
import views.SisDskGUI;

@SuppressWarnings("serial")
public class FinCtaGUI extends GUIInternalFrame implements GUICadInterface{
	
	public JFormattedTextField tfCod;
	public JFormattedTextField tfCodCat;
	public JFormattedTextField tfCodFrn;
	public JTextField tfDesc;
	public JRadioButton rbRc;
	public JRadioButton rbPg;
	public JButton btNFrn;
	public JButton btNCat;
	public JLabel lbFrn;
	public JLabel lbCat;
	public JButton btNovo;
	public JButton btExcluir;
	public JButton btListar;
	public JFormattedTextField tfDtEmi;
	public JFormattedTextField tfDtVen;
	
	public FinCtaGUI() {
		setTitle("Cadastro de Contas");
		setSize(new Dimension(355, 290));
		
		try {
			tfCod = new JFormattedTextField(new MaskFormatter("###"));
			tfCod.setColumns(3);
			tfCod.setBounds(99, 28, 35, 20);
			getContentPane().add(tfCod);			
		} catch (ParseException e) {
			e.printStackTrace();
		}
		JLabel lblCdigo = new JLabel("Cod.:");
		lblCdigo.setBounds(30, 31, 46, 14);
		getContentPane().add(lblCdigo);
		
		tfDesc = new JTextField();
		tfDesc.setColumns(10);
		tfDesc.setBounds(99, 61, 210, 20);
		getContentPane().add(tfDesc);
		
		JLabel lblDescrio = new JLabel("Descri\u00E7\u00E3o:");
		lblDescrio.setBounds(30, 64, 57, 14);
		getContentPane().add(lblDescrio);
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel.setBounds(30, 211, 268, 26);
		getContentPane().add(panel);
		
		btNovo = new JButton("Novo");
		ImageIcon imageIcon = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtNovo));
		Image img = imageIcon.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
		btNovo.setIcon(new ImageIcon(img));
		btNovo.setBounds(1, 1, 80, 23);
		panel.add(btNovo);
		
		btExcluir = new JButton("Excluir");
		btExcluir.setEnabled(false);
		ImageIcon imageIcon2 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtExcluir));
		Image img2 = imageIcon2.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
		btExcluir.setIcon(new ImageIcon(img2));
		btExcluir.setBounds(91, 1, 85, 23);
		panel.add(btExcluir);
		
		btListar = new JButton("Listar");
		ImageIcon imageIcon3 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtListar));
		Image img3 = imageIcon3.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
		btListar.setIcon(new ImageIcon(img3));
		btListar.setBounds(186, 1, 80, 23);
		panel.add(btListar);
		
		JLabel lblCategoria = new JLabel("Categoria:");
		lblCategoria.setBounds(30, 96, 57, 14);
		getContentPane().add(lblCategoria);
		
		try {
			tfCodCat = new JFormattedTextField(new MaskFormatter("###"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		tfCodCat.setColumns(3);
		tfCodCat.setBounds(99, 93, 30, 20);
		getContentPane().add(tfCodCat);
		
		btNCat = new JButton("N");
		btNCat.setBounds(131, 92, 23, 23);
		getContentPane().add(btNCat);
		
		lbCat = new JLabel(".");
		lbCat.setBounds(161, 96, 148, 14);
		getContentPane().add(lbCat);
		
		lbFrn = new JLabel(".");
		lbFrn.setBounds(161, 127, 148, 14);
		getContentPane().add(lbFrn);
		
		btNFrn = new JButton("N");
		btNFrn.setBounds(131, 123, 23, 23);
		getContentPane().add(btNFrn);
		
		try {
			tfCodFrn = new JFormattedTextField(new MaskFormatter("###"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		tfCodFrn.setColumns(3);
		tfCodFrn.setBounds(99, 124, 30, 20);
		getContentPane().add(tfCodFrn);
		
		JLabel lblFornecedor = new JLabel("Fornecedor:");
		lblFornecedor.setBounds(30, 127, 59, 14);
		getContentPane().add(lblFornecedor);
		
		rbRc = new JRadioButton("A Receber");
		rbRc.setBounds(236, 28, 75, 23);
		getContentPane().add(rbRc);
		
		rbPg = new JRadioButton("A Pagar");
		rbPg.setSelected(true);
		rbPg.setBounds(171, 28, 65, 23);
		getContentPane().add(rbPg);
		
		JLabel lblEmisso = new JLabel("Emiss\u00E3o:");
		lblEmisso.setBounds(30, 155, 46, 14);
		getContentPane().add(lblEmisso);
		
		try {
			tfDtEmi = new JFormattedTextField(new MaskFormatter("##/##/####"));
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		tfDtEmi.setBounds(99, 152, 75, 20);
		getContentPane().add(tfDtEmi);
		
		JLabel lblVencimento = new JLabel("Vencimento:");
		lblVencimento.setBounds(30, 183, 65, 14);
		getContentPane().add(lblVencimento);
		
		try {
			tfDtVen = new JFormattedTextField(new MaskFormatter("##/##/####"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		tfDtVen.setBounds(100, 180, 75, 20);
		getContentPane().add(tfDtVen);
	}

	@Override
	public void setListener(ActionListener ac, FocusListener fl, MouseListener ml) {
		tfCod.addFocusListener(fl);
		tfCodCat.addFocusListener(fl);
		tfCodFrn.addFocusListener(fl);
		btNCat.addActionListener(ac);
		btNFrn.addActionListener(ac);
		btNCat.addFocusListener(fl);
		btNFrn.addFocusListener(fl);
		btNovo.addActionListener(ac);
		btExcluir.addActionListener(ac);
		btListar.addActionListener(ac);
	}

	@Override
	public void limpar() {
		tfCod.setText("");
		tfDesc.setText("");
		lbCat.setText("");
		lbFrn.setText("");
		rbPg.setSelected(true);
		rbRc.setSelected(false);
	}

	@Override
	public void setDados(Object obj) {
		Conta cta = (Conta) obj;
		tfCod.setText(cta.getCodigo());
		tfDesc.setText(cta.getDescricao());
		rbPg.setSelected(!cta.isTipo());
		rbRc.setSelected(cta.isTipo());
		tfCodCat.setText(cta.getCategoria().getCodigo());
		tfCodFrn.setText(cta.getFornecedor().getCodigo());
		tfDtEmi.setText(new SimpleDateFormat("dd/MM/yyyy").format(cta.getDtEmi()));
		tfDtVen.setText(new SimpleDateFormat("dd/MM/yyyy").format(cta.getDtVen()));
	}

	@Override
	public Object getDados() {
		Conta cta = new Conta();
		cta.setCodigo(tfCod.getText());
		cta.setDescricao(tfDesc.getText());
		cta.setTipo(rbRc.isSelected());
		cta.setCategoria(Categoria.getCategoria(tfCodCat.getText()));
		cta.setFornecedor(Fornecedor.getFornecedor(tfCodFrn.getText()));
		try {
			java.util.Date dtEmi = new SimpleDateFormat("dd/MM/yyyy").parse(tfDtEmi.getText());
			cta.setDtEmi(new java.sql.Date(dtEmi.getTime()));
			java.util.Date dtVen = new SimpleDateFormat("dd/MM/yyyy").parse(tfDtVen.getText());
			cta.setDtVen(new java.sql.Date(dtVen.getTime()));
		} catch (ParseException e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
		cta.setUsuario(Usuario.getUsuario(GlobalDeclarations.Is_Usuario));
		cta.setDtOpe(new Date(System.currentTimeMillis()));
		return cta;
	}

	@Override
	public JButton getBtNovo() {
		return btNovo;
	}

	@Override
	public JButton getBtExcluir() {
		return btExcluir;
	}

	@Override
	public JButton getBtListar() {
		return btListar;
	}

}
